{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "332126f9-3524-4223-bcc6-24b9818b77b5",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "from mythic import mythic, mythic_classes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "abfa531f-5e7a-400b-910c-92a62106ccbe",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "mythic_instance = await mythic.login(\n",
    "        username=\"mythic_admin\",\n",
    "        password=\"mythic_password\",\n",
    "        server_ip=\"mythic_nginx\",\n",
    "        server_port=7443,\n",
    "        timeout=-1\n",
    "    )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a9b0f1db-5e43-4b08-a4c1-baf48ce07384",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# ################ Create new operator ################\n",
    "op = await mythic.create_operator(\n",
    "    mythic=mythic_instance, username=\"charlie\", password=\"mythic_password\"\n",
    ")\n",
    "print(op)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9e56d3be-9079-4eb1-b10b-27ffbebaccca",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# ################ Get all Operations and their members ################\n",
    "operations = await mythic.get_operations(\n",
    "        mythic=mythic_instance,\n",
    "        custom_return_attributes=\"\"\"\n",
    "        name\n",
    "        id\n",
    "        operators {\n",
    "            id\n",
    "            username\n",
    "        }\n",
    "    \"\"\",\n",
    ")\n",
    "print(operations)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ad605495-7439-4bdc-bdd5-cc4713b13f95",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# ################ Get specific operation and its members ################\n",
    "operation = await mythic.execute_custom_query(\n",
    "    mythic=mythic_instance,\n",
    "    query=\"\"\"\n",
    "    query specificOperation{\n",
    "        operation(where: {name: {_eq: \"Operation Chimera\"}}) {\n",
    "            id\n",
    "            name\n",
    "            operators {\n",
    "                id\n",
    "                username\n",
    "            }\n",
    "        }\n",
    "    }\n",
    "\"\"\",\n",
    ")\n",
    "print(operation)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4fde16d7-9bb3-4524-a9de-afca4b796cb6",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# ################ Create new operation ################\n",
    "await mythic.create_operation(mythic=mythic_instance, operation_name=\"Operation Test\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3f4515f0-db04-494b-9f45-59ffa52edfc5",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# ################ Add User to Operation ################\n",
    "try:\n",
    "    resp = await mythic.add_operator_to_operation(\n",
    "        mythic=mythic_instance,\n",
    "        operation_name=\"Operation Chimera\",\n",
    "        operator_username=\"charlie\",\n",
    "    )\n",
    "    print(resp)\n",
    "except Exception as e:\n",
    "    print(f\"Got exception adding user to operation: {e}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ecceac93-cc73-406f-8b44-145a58e5b7d9",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# ################ Update Operator View Mode ################\n",
    "try:\n",
    "    await mythic.update_operator_in_operation(\n",
    "        mythic=mythic_instance,\n",
    "        operation_name=\"Operation Chimera\",\n",
    "        operator_username=\"charlie\",\n",
    "        view_mode=\"spectator\",\n",
    "    )\n",
    "except Exception as e:\n",
    "    print(f\"Got exception updating user in operation: {e}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d29f9e17-0d14-469f-8817-22e2adc16efd",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# ################ Update Operation ################\n",
    "try:\n",
    "    resp = await mythic.update_operation(\n",
    "        mythic=mythic_instance,\n",
    "        operation_name=\"Operation Chimera\",\n",
    "        lead_operator_username=\"charlie\",\n",
    "        channel=\"#mythic-notifications\",\n",
    "    )\n",
    "    print(resp)\n",
    "except Exception as e:\n",
    "    print(f\"Got exception updating operation: {e}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b8df3ca5-5276-4442-86fa-35eab4ea0b04",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# ################ Remove User from Operation ################\n",
    "try:\n",
    "    resp = await mythic.remove_operator_from_operation(\n",
    "        mythic=mythic_instance,\n",
    "        operation_name=\"Operation Chimera\",\n",
    "        operator_username=\"charlie\",\n",
    "    )\n",
    "    print(resp)\n",
    "except Exception as e:\n",
    "    print(f\"Got exception removing user from operation: {e}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e1eaca58-e6c0-4012-8b8a-bfd3fb847514",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# ################ Set User Current Operation ###############\n",
    "# must be global admin to do this for somebody other than yourself\n",
    "await mythic.update_current_operation_for_user(\n",
    "    mythic=mythic_instance, operator_id=2, operation_id=7\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8b4c9137-ddbf-4c1b-8a03-441d0dec7262",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# ################ Update Operator Information ###############\n",
    "# must be global admin to do this for somebody other than yourself\n",
    "resp = await mythic.execute_custom_query(\n",
    "    mythic=mythic_instance,\n",
    "    query=\"\"\"\n",
    "    mutation updateOperatorAdminStatus($username: String!, $admin: Boolean){\n",
    "        update_operator(_set: {admin: $admin}, where: {username: {_eq: $username}}){\n",
    "            returning {\n",
    "                id\n",
    "                admin\n",
    "            }\n",
    "        }\n",
    "    }\n",
    "    \"\"\",\n",
    "        variables={\"username\": \"charlie\", \"admin\": True},\n",
    "    )\n",
    "print(resp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "912f3f4a-0284-4200-b71f-007fa2822355",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
